#define _XOPEN_SOURCE  1
#define _XOPEN_SOURCE_EXTENDED 1
#define _BSD_SOURCE  1
#define _SVID_SOURCE  1
#define _POSIX_SOURCE  1
#include <header.h>
#include "zemaphore.h"

typedef struct pool_s{
    // 用来保存线程id
    pthread_t putFileThreadt;
	pthread_t getFileThreadt;

    // 信号量
    zem_t putfile;
	zem_t getfile;

    // 0运行，1退出
	// TODO 需要实现有序退出
    int exitFlag;

}pool_t;

void *putFileThread(void* arg){
	pool_t *pool = (pool_t*)arg;

	zem_wait(&pool->putfile);	

	int i = 0;
	while (i < 5){
		i++;
		printf("上传中\n");
		sleep(1);
	}
	printf("上传完毕\n");
	

	return NULL;
}

void *getFileThread(void* arg){
	pool_t *pool = (pool_t*)arg;
	zem_wait(&pool->getfile);
	printf("下载完毕\n");
	return NULL;
}


/* 初始化信息，创建线程池 */
int initPool(pool_t *pool){

	// 初始化信号量,初值为0
	zem_init(&pool->getfile, 0);
	zem_init(&pool->putfile, 0);

	pthread_create(&pool->putFileThreadt, NULL, putFileThread, (void *)pool);
	pthread_create(&pool->getFileThreadt, NULL, getFileThread, (void *)pool);

    pool->exitFlag = 0;
    return 0;
}

int destoryPool(pool_t *pool){
	pthread_join(pool->getFileThreadt, NULL);
	pthread_join(pool->putFileThreadt, NULL);

	zem_destory(&pool->getfile);
	zem_destory(&pool->putfile);
	

	return 0;
}


int main(int argc, char* argv[]){
	pool_t pool;
	initPool(&pool);

	zem_post(&pool.putfile);
	zem_post(&pool.getfile);

	destoryPool(&pool);
	return 0;
}